//
//  FDAlertView.m
//  UIButton测试
//
//  Created by xiaofang wang on 2019/1/2.
//  Copyright © 2019年 zhiyuan. All rights reserved.
//1-2个人资料专用弹框


#import "FDCustomAlertView.h"

//屏幕宽高
//#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
//#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

//状态栏高度 (20 / 44(iPhoneX,XR,XS,XS MAX))
#define RB_STATUSBAR_HEIGHT ([UIApplication sharedApplication].statusBarFrame.size.height)
#define RB_IS_iPhoneX ((RB_STATUSBAR_HEIGHT > 20.0) ? YES : NO)
//底部安全区域高度
#define RB_BOTTOM_MARGIN (RB_IS_iPhoneX ? 34.0 : 0.0)

//tv的高度 100
#define kTvHeight 100
//间距10
#define kMargin10 10
//取消按钮高度
#define kCancelBtnHeight 50
//圆角弧度
#define kCornerRadius 10


@interface FDAlertCell : UITableViewCell
/**title*/
@property (nonatomic, strong) UILabel *titleLb;
@end
@implementation FDAlertCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setupUI {
    [self.contentView addSubview:self.titleLb];
}

- (void)setFrame:(CGRect)frame {
    frame.size.height -= 1;
    [super setFrame:frame];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-2);
    }];
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLb;
}

@end





@interface FDCustomAlertView()<UITableViewDelegate,UITableViewDataSource>

/**灰色背景*/
@property (nonatomic, strong) UIView *bgView;



/**alertview 以下子控件都添加在bgView上*/
@property (nonatomic, strong) UIView *alertView;
/**tableView 2个cell*/
@property (nonatomic, strong) UITableView *tv;
/**取消按钮一个按钮*/
@property (nonatomic, strong) UIButton *cancelButton;
/**数据源*/
@property (nonatomic, strong) NSArray *titleArr;
/**block回调*/
@property (nonatomic, copy) FDAlertViewBlock block;

@end

@implementation FDCustomAlertView

#pragma mark - 接口
+ (void)showAlertWithTitleArr:(NSArray *)arr complectionBlock:(FDAlertViewBlock)block {
    
    FDCustomAlertView *alertView = [[FDCustomAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    //私有属性保存外界传进来的数据
    alertView.titleArr = arr;
    alertView.block = block;
    
    [alertView showAlertViewAnimation];//显示
}


//就是一个全屏的
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.frame = [UIScreen mainScreen].bounds; //全屏

    //给bgview添加手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBgViewGesture:)];
    [self.bgView addGestureRecognizer:tapGes];
    [self addSubview:self.bgView]; //添加到self
    [self addSubview:self.alertView];//添加到self

    
    
    [self.alertView addSubview:self.tv]; //添加到alertView上
    [self.alertView addSubview:self.cancelButton]; //添加到alertView上
}

#pragma mark -   UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FDAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FDAlertCell"];
    cell.titleLb.text = self.titleArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titleArr[indexPath.row];
    //回调
    if (self.block) {
        self.block(indexPath.row, title);
    }
    
    //消失
    [self dismissAlertViewAnimation];
}

#pragma mark - private 显示、隐藏弹框
- (void)showAlertViewAnimation {

    //取出keywindow
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self];
    
    //修改y轴位置
    CGRect rect = self.alertView.frame;
    rect.origin.y = SCREEN_HEIGHT;
    self.alertView.frame = rect;
    
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect2 = self.alertView.frame;
        rect2.origin.y -= (kTvHeight + kMargin10*2 + kCancelBtnHeight);
        self.alertView.frame = rect2;
    }];
}


- (void)dismissAlertViewAnimation {
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kTvHeight + kMargin10 * 2 + kCancelBtnHeight;
        self.alertView.frame = rect;
        self.alertView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 点击事件
- (void)clickBgViewGesture:(UITapGestureRecognizer *)tap {
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
    
    [self dismissAlertViewAnimation];//消失
}
- (void)clickCancelAction {
    [self dismissAlertViewAnimation];
}

#pragma mark - getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; //全屏
        _bgView.backgroundColor = [UIColor rb_colorHex:0x000000 alpha:0.5];
    }
    return _bgView;
}

- (UIView *)alertView {
    if (!_alertView) {
        //100 //10 //50 //10
        CGFloat h = kTvHeight + kMargin10 + kCancelBtnHeight + kMargin10 + RB_BOTTOM_MARGIN;
        CGFloat y = SCREEN_HEIGHT - h;
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, h)];
    }
    return _alertView;
}


- (UITableView *)tv {
    if (!_tv) {
        CGRect tvframe = CGRectMake(kMargin10, 0, SCREEN_WIDTH - kMargin10*2, kTvHeight);
        _tv = [[UITableView alloc] initWithFrame:tvframe style:(UITableViewStylePlain)];
        _tv.scrollEnabled = NO;
        
        _tv.backgroundColor = kMainLightGrayColor;
        
        //设置代理
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册cell
        [_tv registerClass:[FDAlertCell class] forCellReuseIdentifier:@"FDAlertCell"];
        _tv.separatorStyle = UITableViewCellSelectionStyleNone;
        
        //圆角
        _tv.layer.masksToBounds = YES;
        _tv.layer.cornerRadius  = kMargin10;
    }
    return _tv;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        //国际化适配
        NSString *qxStr = NSLocalizedString(Cancel, @"取消");
        [_cancelButton setTitle:qxStr forState:(UIControlStateNormal)];
        
        _cancelButton.backgroundColor = [UIColor whiteColor];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_cancelButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
        CGFloat y = kTvHeight + kMargin10;
        CGFloat h = kCancelBtnHeight;
        _cancelButton.frame = CGRectMake(kMargin10, y, SCREEN_WIDTH - 2*kMargin10, h);
        
        // 圆角
        _cancelButton.layer.masksToBounds = YES;
        _cancelButton.layer.cornerRadius = kMargin10;
        
        // 点击事件
        [_cancelButton addTarget:self action:@selector(clickCancelAction) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _cancelButton;
}
@end

